package com.dick.base.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dick.base.aop.SmartQuery;
import com.dick.base.mapper.BusinessBoardMapper;
import com.dick.base.model.BusinessBoard;
import com.dick.base.parameter.BoardAddParameter;
import com.dick.base.util.DateUtil;
import com.dick.base.util.IdUtil;
import com.dick.base.util.PageInfo;
import com.dick.base.util.SmartQueryUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 库存板服务
 */
@Service
public class BoardService {

    @Autowired
    private BusinessBoardMapper businessBoardMapper;

    /**
     * 新增库存板
     * @param parameter
     */
    @Transactional
    public void boardAdd(BoardAddParameter parameter) {
        BusinessBoard businessBoard = new BusinessBoard();
        businessBoard.setMaterial(parameter.getMaterial());
        businessBoard.setThickness(parameter.getThickness());
        businessBoard.setWidth(parameter.getWidth());
        businessBoard.setLength(parameter.getLength());
        List<BusinessBoard> boards = businessBoardMapper.selectList(new QueryWrapper<>(businessBoard));
        if (boards.isEmpty()) { //新增
            businessBoard.setQuantity(parameter.getQuantity());
            businessBoard.setId(IdUtil.nextId());
            businessBoard.setCreateTime(DateUtil.now());
            businessBoardMapper.insert(businessBoard);
        } else { //添加已有库存
            businessBoardMapper.increaseQuantity(boards.get(0).getId(), parameter.getQuantity());
        }
    }

    /**
     * 分页查询
     * @param businessBoard
     * @param pageInfo
     * @return
     */
    @SmartQuery(clazz = BusinessBoard.class)
    public Page<BusinessBoard> boardPage(BusinessBoard businessBoard, PageInfo pageInfo) {
        return businessBoardMapper.selectPage(SmartQueryUtil.getPage(), SmartQueryUtil.getQueryWrapper());
    }
}
